package config

import (
	"fmt"
	"gopkg.in/yaml.v3"
	"os"
)

type Config struct {
	Appid       string      `yaml:"appid"`
	Portmonitor Portmonitor `yaml:"portmonitor"`
	Alarm       Alarm       `yaml:"alarm"`
	Log         Log         `yaml:"log"`
	Server      Server      `yaml:"server"`
}
type Portmonitor struct {
	PollSecond   int      `yaml:"pollsecond"`
	Ports        []int    `yaml:"ports"`
	StartCommand []string `yaml:"startsh"`
}
type Alarm struct {
	Webhook        string `yaml:"webhook"`
	NoticeTemplate string `yaml:"noticetemplate"`
}
type Log struct {
	LogFile    string `yaml:"filepath"`
	MaxHistory int    `yaml:"maxhistory"`
}
type Server struct {
	Port string `yaml:"port"`
}

var configObj *Config

// 获取配置
func GetConfig() *Config {
	return configObj
}

func init() {
	if configObj == nil {
		configObj = readConfig()
	}
}

/*
*
读取config.json配置文件
*/
func readConfig() *Config {
	dataBytes, err := os.ReadFile("config.yaml")
	if err != nil {
		fmt.Println("读取config.yaml文件失败：", err)
		return nil
	}
	config := Config{}
	err = yaml.Unmarshal(dataBytes, &config)
	if err != nil {
		fmt.Println("解析config.yaml文件失败：", err)
		return nil
	}
	return &config
}
